package pers.zh.parking.config;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;

import com.jess.arms.base.BaseApplication;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.di.module.ClientModule;
import com.jess.arms.di.module.GlobalConfigModule;
//import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy;
import com.jess.arms.integration.ConfigModule;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import pers.zh.parking.R;
import pers.zh.parking.common.ApiService;
import pers.zh.parking.utils.TimberUtils;
import pers.zjc.commonlibs.util.ToastUtils;

import static pers.zh.parking.common.Const.HttpTime.CONNECT_TIMEOUT;
import static pers.zh.parking.common.Const.HttpTime.READ_TIMEOUT;
import static pers.zh.parking.common.Const.HttpTime.WRITE_TIMEOUT;

//import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy;

public class GlobalConfiguration implements ConfigModule {

    private static BaseApplication INSTANCE;

    public static BaseApplication getInstance() {
        return INSTANCE;
    }

    @Override
    public void applyOptions(@NotNull Context context, GlobalConfigModule.Builder builder) {
        INSTANCE = (BaseApplication)context;
        //使用 builder 可以为框架配置一些配置信息
        builder.baseurl(ApiService.APP_DOMAIN).cacheFile(new File("cache"))
               //               .imageLoaderStrategy(new GlideImageLoaderStrategy())
               .okhttpConfiguration(new ClientModule.OkhttpConfiguration() {
                   @Override
                   public void configOkhttp(@NonNull Context context,
                                            @NonNull OkHttpClient.Builder builder) {
                       builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                              .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                              .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                              .retryOnConnectionFailure(true);
                   }
               });
//               .retrofitConfiguration((context1, builder1) -> { builder1.addConverterFactory(GsonConverterFactory.create());
//        });
    }

    //向 Application的 生命周期中注入一些自定义逻辑
    @Override
    public void injectAppLifecycle(@NotNull Context context,
                                   @NotNull List<AppLifecycles> lifecycles) {
        //设置Timber log自动在apk为debug版本时打开，在release版本时关闭
        TimberUtils.setLogAuto();
        configToast(context);
        configRefresh();
    }

    private void configRefresh() {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(
                        context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        //        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
        //            @Override
        //            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
        //                //指定为经典Footer，默认是 BallPulseFooter
        //                return new ClassicsFooter(context).setDrawableSize(20);
        //            }
        //        });
    }

    private void configToast(@NotNull Context context) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.setBgColor(context.getColor(R.color.black));
        ToastUtils.setMsgColor(context.getColor(R.color.white));
    }

    @Override
    public void injectActivityLifecycle(@NotNull Context context,
                                        @NotNull List<Application.ActivityLifecycleCallbacks> lifecycles) {
        //向 Activity 的生命周期中注入一些自定义逻辑
    }

    @Override
    public void injectFragmentLifecycle(@NotNull Context context,
                                        @NotNull List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
        //向 Fragment 的生命周期中注入一些自定义逻辑
    }

}